package cn.com.cookie;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "addCookieServlet", value = "/addCookieServlet")
public class addCookieServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        //创建Cookie对象
        Cookie cookie = new Cookie("mycookie", "第一个cookie对象");
        //设置有效时间 1天
        cookie.setMaxAge(60*60*24);
        //添加到浏览器
        response.addCookie(cookie);
        //打印Cookie对象
        PrintWriter out = response.getWriter();
        out.println(cookie.getName()+"="+cookie.getValue());


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}